<?php

/* CSR attributes :
 * csr_id
 * name
 * contact
 * plasa_id 
 */

class csrmodel extends CI_Model {

    // add csr
    public function add_csr($name = null, $contact = null, $plasa_id = null) {
        if ($name != null && $contact != null && $plasa_id != null) {
            $add_csr = array(
                'name' => $name,
                'contact' => $contact,
                'plasa_id' => $plasa_id,
            );
            return $this->add_csr_arr($add_csr);
        }
    }

    // add csr tuple
    public function add_csr_arr($csr_tuple = null) {
        if ($csr_tuple != null) {
            $insert = $this->db->insert('csr', $csr_tuple);
			if($insert) $insert = $this->db->insert_id();
			
            return $insert;
        }
    }

    // get csr info based on id
    public function get_csr_info($csr_id = null) {
        if ($csr_id != null) {
            $this->db->where('csr_id', $csr_id);
            $query = $this->db->get('csr');
            return $query;
        }
    }

    // get info of all plasa
    public function get_all_csr() {
        $this->db->select('csr.csr_id AS csr_id, csr.name AS csr_name, csr.contact AS csr_contact, csr.plasa_id AS plasa_id, plasa.name AS plasa_name');
        $this->db->from('csr');
        $this->db->join('plasa', 'csr.plasa_id = plasa.plasa_id', 'INNER JOIN');
        $query = $this->db->get();

        //return $this->db->last_query();
        //return print_r($query->result_array());
        return $query;
    }

    public function get_all_csr_by_plasa($plasa_id) {
        if ($plasa_id != null) {
            $this->db->select('csr.csr_id AS csr_id, csr.name AS csr_name, csr.contact AS csr_contact, csr.plasa_id AS plasa_id, plasa.name AS plasa_name');
            $this->db->from('csr');
            $this->db->where('csr.plasa_id', $plasa_id);
            $this->db->join('plasa', 'csr.plasa_id = plasa.plasa_id', 'INNER JOIN');
            $query = $this->db->get();
            //return $this->db->last_query();
            return $query;
        }
    }
    
    public function get_all_csr_by_plasa_nojoin($plasa_id) {
        if ($plasa_id != null) {
            $this->db->select('csr.csr_id AS csr_id, csr.name AS csr_name, csr.contact AS csr_contact, csr.plasa_id');
            $this->db->from('csr');
            $this->db->where('csr.plasa_id', $plasa_id);
            $query = $this->db->get();
            //return $this->db->last_query();
            return $query;
        }
    }

    public function update_csr($csr_id = null, $name = null, $contact = null, $plasa_id = null) {

        if ($csr_id !== null) {

            $stack = array();

            if ($name !== null) {
                $stack['name'] = $name;
            }

            if ($contact !== null) {
                $stack['contact'] = $contact;
            }

            if ($plasa_id !== null) {
                $stack['plasa_id'] = $plasa_id;
            }

            if ($stack !== null) {
                //print_r($stack);
                return $this->update_csr_arr($csr_id, $stack);
            }
        }
    }

    //update plasa by an array of plasa information
    public function update_csr_arr($csr_id = null, $update_tuple = null) {
        if (($csr_id !== null) && ($update_tuple !== null)) {
            $this->db->where('csr_id', $csr_id);
            return $this->db->update('csr', $update_tuple);
        }
    }

    //delete a csr
    public function delete_csr($csr_id = null) {
        if ($csr_id !== null)
            $this->db->delete('csr', array('csr_id' => $csr_id));
        return $this->db->affected_rows();
    }

}

?>
